package com.example.mallauthserver.controller;

import com.common.utils.R;
import com.example.mallauthserver.controller.vo.LoginVO;
import com.example.mallauthserver.feign.MemberFeginService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author guanglin.ma
 * @date 2024-01-03 15:27
 */
@Controller
public class LoginController {

    @Autowired
    MemberFeginService memberFeginService;

    @Autowired
    ObjectMapper objectMapper;

    @PostMapping({"/login"})
    public String login(LoginVO loginVO, RedirectAttributes redirectAttributes, HttpSession session) throws IOException {
        R r = memberFeginService.login(loginVO);
        if (r.getCode() == 0) {
            session.setAttribute("login",r.getData());
            // 表示登录成功
            return "redirect:http://mall.mg.com/home";
        }

        redirectAttributes.addAttribute("errors", r.getMsg());

        // 表示登录失败,重新跳转到登录页面
        return "redirect:http://auth.mg.com/login.html";
    }
}
